package com.aizhixin.lab.project.template.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.template.domain.ProjectModuleDomain;
import com.aizhixin.lab.project.template.domain.ProjectRoleDomain;
import com.aizhixin.lab.project.template.entity.ModuleTask;
import com.aizhixin.lab.project.template.entity.Project;
import com.aizhixin.lab.project.template.entity.ProjectModule;
import com.aizhixin.lab.project.template.entity.ProjectRole;
import com.aizhixin.lab.project.template.repository.ProjectModuleRepository;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectModuleService {
    @Autowired
    private ProjectModuleRepository moduleRepository;
    @Autowired
    private ModuleTaskService moduleTaskService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    public void updateSort(List<String> ids){
        int i=1;
        for (String id : ids) {
            ProjectModule projectModule = moduleRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
            projectModule.setModuleNo(i);
            moduleRepository.save(projectModule);
            i++;
        }
    }

    public ProjectModule save(ProjectModuleDomain domain) {
        List<ProjectRole> projectRoles=new ArrayList<>();
        if (domain.getProjectRoleDomainList()!=null) {
            for (ProjectRoleDomain roleDomain : domain.getProjectRoleDomainList()) {
                ProjectRole projectRole = new ProjectRole();
                BeanUtils.copyProperties(roleDomain, projectRole);
                projectRoles.add(projectRole);
            }
        }
        if (null == domain.getId() || "".equals(domain.getId())) {
            ProjectModule projectModule = new ProjectModule();
            BeanUtils.copyProperties(domain,projectModule);
            projectModule.setId(UUID.randomUUID().toString());
            projectModule.setCreatedDate(new Date());
            projectModule.setDeleteFlag(DataValidity.VALID.getState());
            Integer integer = moduleRepository.countByProjectIdAndDeleteFlag(projectModule.getProjectId(), DataValidity.VALID.getState());
            projectModule.setModuleNo(integer+1);
            projectModule.setProjectRoleList(projectRoles);
            projectModule.setTaskNum(0);
            Project project = projectService.findProject(projectModule.getProjectId());
            project.setModuleNum(project.getModuleNum()+1);
            projectService.save(project);
            return moduleRepository.save(projectModule);
        }
        ProjectModule one = moduleRepository.findOne(domain.getId());
        one.setProjectRoleList(projectRoles);
        domain.setTaskNum(one.getTaskNum());
        BeanUtils.copyProperties(domain,one);
        return moduleRepository.save(one);
    }

    public List<ProjectModuleDomain> findModuleByProject(String projectId){
        List<ProjectModule> moduleList = moduleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());
        List<ProjectModuleDomain> moduleDomainList=new ArrayList<>();
        for(ProjectModule projectModule:moduleList){
            ProjectModuleDomain moduleDomain = new ProjectModuleDomain();
            BeanUtils.copyProperties(projectModule,moduleDomain);
            List<ProjectRoleDomain> projectRoleDomainList=new ArrayList<>();
            for (ProjectRole role:projectModule.getProjectRoleList()){
                ProjectRoleDomain roleDomain = new ProjectRoleDomain();
                BeanUtils.copyProperties(role,roleDomain);
                projectRoleDomainList.add(roleDomain);
            }
            moduleDomain.setProjectRoleDomainList(projectRoleDomainList);
            moduleDomainList.add(moduleDomain);
        }
        return moduleDomainList;
    }
    public ProjectModuleDomain findModuleById(String id){
        ProjectModule projectModule = moduleRepository.findOne(id);
        ProjectModuleDomain moduleDomain = new ProjectModuleDomain();
        BeanUtils.copyProperties(projectModule,moduleDomain);
        List<ProjectRoleDomain> roleDomainList=new ArrayList<>();
        for (ProjectRole projectRole:projectModule.getProjectRoleList()){
            ProjectRoleDomain roleDomain = new ProjectRoleDomain();
            BeanUtils.copyProperties(projectRole,roleDomain);
            roleDomainList.add(roleDomain);
        }
        moduleDomain.setProjectRoleDomainList(roleDomainList);
        return moduleDomain;
    }
    public Map<String, Object> deleteModule(String id){
        Map<String, Object> result = new HashMap<String, Object>();
        Integer taskNum = moduleTaskService.countTaskNum(id);
        if (taskNum>0){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.ERROR,"请先删除子任务");
        }else {
            ProjectModule projectModule = moduleRepository.findByIdAndDeleteFlag(id,DataValidity.VALID.getState());
            if (!Objects.isNull(projectModule)) {
                Project project = projectService.findProject(projectModule.getProjectId());
                projectModule.setDeleteFlag(DataValidity.INVALID.getState());
                moduleRepository.save(projectModule);
                if (!Objects.isNull(project)) {
                    project.setModuleNum(moduleRepository.countByProjectIdAndDeleteFlag(project.getId(),DataValidity.VALID.getState()));
                    projectService.save(project);
                    updateSort(project.getId());
                }
                result.put(ApiReturnConstants.SUCCESS, true);
                result.put(ApiReturnConstants.DATA, "删除成功");
            }else {
                result.put(ApiReturnConstants.SUCCESS, false);
                result.put(ApiReturnConstants.DATA, "模块不存在");
            }
        }
        return result;
    }
    public void updateSort(String projectId){
        List<ProjectModule> projectModuleList = moduleRepository.findByProjectIdAndDeleteFlagOrderByModuleNo(projectId, DataValidity.VALID.getState());
        for (int i=0;i<projectModuleList.size();i++){
            projectModuleList.get(i).setModuleNo(i+1);
        }
        moduleRepository.save(projectModuleList);
    }
    public ProjectModule findById(String id){
       return moduleRepository.findOne(id);
    }
    public ProjectModule save(ProjectModule projectModule){
        return moduleRepository.save(projectModule);
    }
//    private void updateTaskNo(String id,Integer taskNo){
//        String sql="UPDATE t_project_module tpm SET tpm.module_no=tpm.module_no-1 WHERE tpm.project_id= '"+id+"' AND tpm.module_no>"+taskNo;
//        pageJdbcUtil.update(sql);
//    }
}
